package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Student;
import com.itheima.service.StudentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    private StudentMapper studentMapper;

    /**
     * 学员列表查询
     */
    @Override
    public PageBean<Student> getStudent(String name, Integer degree, Integer clazzId, Integer page, Integer pageSize) {
        //开启分页
        PageHelper.startPage(page,pageSize);
        //查询列表：不需要传参起始索引与查询数量
        Page<Student> studentPage = studentMapper.page(name,degree,clazzId);

        return new PageBean<>((int)studentPage.getTotal(),studentPage);
    }

    /**
     * 新增学生信息
     */
    @Override
    public void addStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.insertStudent(student);
    }

    /**
     * 根据ID查询学生
     */
    @Override
    public Student getStudentById(Integer id) {
        Student stu = studentMapper.selectStuById(id);
        return stu;
    }

    /**
     * 修改学员
     */
    @Override
    public void putStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateStudent(student);
    }

    /**
     * 删除学生信息
     */
    @Override
    public void deleteStu(List<Integer> ids) {
        studentMapper.deleteStu(ids);
    }

    /**
     * 违纪处理
     */
    @Override
    public void violationStu(Integer id, Integer score) {
        studentMapper.vioStu(id,score);
    }
}
